package com.example.siyun.service.impl.productcategroyserviceimpl;

import com.example.siyun.mapper.productcategroymapper.ProductCategroyMapper;
import com.example.siyun.pojo.JsProductCategroy;
import com.example.siyun.pojo.TreeNodes;
import com.example.siyun.service.productcategroyservice.ProductCategroyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/*
 * 商品列表
 * @author：刘宝龙
 *@Date: 2022-10-12
 * */

@Service
public class ProductCategroyServiceImpl implements ProductCategroyService {
    @Autowired
    private ProductCategroyMapper productCategroyMapper;

    /**
     * 左树的增删改查
     * 根据cid展示树
     * @param parentId
     * @return
     */
    @Override
    public List<TreeNodes> getpid(int parentId) {
        List<TreeNodes> list = new ArrayList<>();
        List<JsProductCategroy> cates = productCategroyMapper.getpid(parentId);
        for (JsProductCategroy jsProduct : cates) {
            TreeNodes treeNodes = new TreeNodes();
            treeNodes.setId(jsProduct.getCategrayId());
            treeNodes.setTitle(jsProduct.getTitle());

            List<TreeNodes> child = getpid(jsProduct.getCategrayId());
            treeNodes.setChildren(child);
            treeNodes.setSpread(true);

            list.add(treeNodes);
        }

        return list;
    }

    /**
     * 修改
     * @param treeNodes
     * @throws Exception
     */
    @Override
    public void updateTreeName(TreeNodes treeNodes) throws Exception {
        int num = productCategroyMapper.uqdateTree(treeNodes.getTitle(),
                treeNodes.getId());
        if (num == 0) {
            throw new Exception();
        }
    }

    /**
     * 删除
     * @param categrayId
     * @throws Exception
     */
    @Override
    public void delTreeNodes(Integer categrayId) throws Exception {
        int i = productCategroyMapper.updateIsDel(categrayId);
        if (i == 0) {
            throw new Exception();
        }
    }

    /**
     * 添加
     * @param jsProductCategroy
     * @throws Exception
     */
    @Override
    public void addTreeNodes(JsProductCategroy jsProductCategroy) throws Exception {
        int i = productCategroyMapper.insertTree(jsProductCategroy);
        if (i == 0) {
            throw new Exception();
        }

    }

}
